//
//  EFengPersonalShareController.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/3/14.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EFengPersonalShareController.h"
#import "UIBarButtonItem+category.h"
#import "EFengShareButton.h"
#import "EFengShareCell.h"
#import "EFengShare.h"
#import "EFengComposeController.h"
#import "CXmppRequestMgr.h"
#import "UIUtil.h"
#import "EFengShareFrame.h"
#import "EFengContent.h"
#import "CContactMgr.h"
#import "CFolderMgr.h"
#import "CContactModel.h"
#import "UIUtil.h"
#import "CShareMgr.h"
#import "CShareCommentMgr.h"
#import "MBProgressHUD+WZ.h"
#import "MJExtension.h"
#import "UIUtil.h"
#import "CAttentionMgr.h"
#import "CMineAttentionContact.h"
#import "EFengShareCommentToolBar.h"
#import "EFengShareEmotionView.h"
#import "MJRefresh.h"
#import "RIButtonItem.h"
#import "UIActionSheet+Blocks.h"
#import "CShareCommentDelMgr.h"
#import "CShareDelMgr.h"
#import "EFengShareDel.h"
#import "EFengShareCommentDel.h"
#import "CSharePersonMgr.h"
#import "JKAlertDialog.h"
#import "EFengChatListView.h"
#import "CHChatRoomBase.h"
#import "GroupMemberDetailVC.h"
@interface EFengPersonalShareController ()<UITableViewDataSource, UITableViewDelegate, EFengShareCellDelegate, EFengShareCommentToolBarDelegate,EFengChatListViewDelegate,UIGestureRecognizerDelegate>

WeakProperty UIView *topView;
WeakProperty EFengShareButton *selectedButton;

StrongProperty NSMutableArray *dataArray;
StrongProperty NSMutableArray *shareDataArray;

StrongProperty UITableView *tableView;

StrongProperty CContactModel *Contact;
StrongProperty CContactMgr *conMgr;
StrongProperty CContactModel *curContact;

StrongProperty CShareMgr *shareMgr;
StrongProperty CShareCommentMgr *commentMgr;
StrongProperty CShareCommentDelMgr *shareCommentDelMgr;
StrongProperty CShareDelMgr *shareDelMgr;
StrongProperty CAttentionMgr *attenMgr;
StrongProperty CSharePersonMgr *personShareMgr;
//更多选项
StrongProperty EFengChatListView *moreView;
StrongProperty EFengContent *selectedComment;
StrongProperty EFengShare *selectedShare;
StrongProperty EFengShareFrame *maxShare;
StrongProperty EFengShareFrame *minShare;
//通过用户id 获取关注的人
StrongProperty CAttentionMgr *attentionMgr;
@property(nonatomic,weak)UILabel *noticeLable;
@property(nonatomic,assign)BOOL isShowMoreView;
@end


@implementation EFengPersonalShareController



#pragma mark 懒加载
- (CSharePersonMgr *)personShareMgr
{
    if (_personShareMgr == nil) {
        _personShareMgr = [[CSharePersonMgr alloc]init];
    }
    return _personShareMgr;
}
- (EFengChatListView *)moreView
{
    
    
        if (_moreView == nil) {
            _moreView = [[EFengChatListView alloc] initWithFrame:CGRectZero];
            _moreView.chatListDelegate = self;
            [self.view addSubview:_moreView];
        }
        return _moreView;
    
}
- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)shareDataArray
{
    if (_shareDataArray == nil) {
        _shareDataArray = [NSMutableArray array];
    }
    return _shareDataArray;
}


- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:IMDIDCOMPOSESHARE object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPDelComment object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPReceiveComment object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPDelShare object:nil];
    

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(WZAttributedActionDidClickForShowPersonalShare:) name:WZAttributedActionDidClick object:nil];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    if (IsIOS7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadNewData) name:IMDIDCOMPOSESHARE object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleComment) name:K_XMPPReceiveComment object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleComment) name:K_XMPPDelComment object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleShare:) name:K_XMPPDelShare object:nil];
    

    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    
    
    
    self.shareMgr = [[CShareMgr alloc] init];
    self.commentMgr = [[CShareCommentMgr alloc] init];
    self.conMgr = [[CContactMgr alloc] init];
    self.attenMgr = [[CAttentionMgr alloc] init];
    self.shareCommentDelMgr = [[CShareCommentDelMgr alloc] init];
    self.shareDelMgr = [[CShareDelMgr alloc] init];
    self.Contact = [self.conMgr loadContactWithUserId:self.userId];
    self.curContact = [self.conMgr loadCurContact];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"btn-more" title:nil target:self sel:@selector(releaseShare) titleFont:Font18 titleColor:WhiteColor];

    NSString *tit = nil;
    if ([self.curContact.UserID isEqualToString:self.Contact.UserID]) {
        tit = @"我的分享";
        
    } else {
        tit = [NSString stringWithFormat:@"%@的分享",self.Contact.UserName];
        
    }
    
    [self.navigationItem setNewTitleCenter:tit];
    
    self.tableView.frame = CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT - 64.0f);
    [self.view addSubview:self.tableView];
    self.isShowMoreView = NO;
    //增加下拉刷新
    
    __block EFengPersonalShareController *weakSelf = self;
//    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        [weakSelf loadNewData];
//    }];
//    header.lastUpdatedTimeLabel.hidden = YES;
//    header.stateLabel.hidden = YES;
//    self.tableView.header = header;
    //增加上拉加载更多
    
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadMoreData];
    }];
    [footer setTitle:@"加载更多..." forState:MJRefreshStateRefreshing];
    
    
    self.tableView.footer = footer;
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
    [self.tableView addGestureRecognizer:tap];
    //bug 8999 fixed by lixu
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = ClearColor;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = Font16;
    label.textColor = RGBCOLORWITH16COLOR(0xb1b1b1);
    label.text = @"这里暂时空空的～";
    label.frame = CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, Font16.lineHeight);
    label.center = self.view.center;
    [self.view addSubview:label];
    self.noticeLable = label;
    self.attentionMgr = [[CAttentionMgr alloc]init];
    [self.view bringSubviewToFront:self.moreView];
    [self loadData];
    
}
- (void)handleComment
{
    [self.dataArray removeAllObjects];
    [self.shareDataArray enumerateObjectsUsingBlock:^(EFengShare *obj, NSUInteger idx, BOOL *stop) {
        EFengShareFrame *shareF = [[EFengShareFrame alloc] init];
        
        obj.contentReply = [self.commentMgr getShareCommentWithShareId:obj.shareId];
        obj.isZan = [self.commentMgr isHasZanIdWithShareId:obj.shareId userId:self.curContact.UserID];
        obj.praiseArray = [self.commentMgr getSharePraiseWithShareId:obj.shareId];
        if ([self.curContact.UserID isEqualToString:obj.releaseUserId]) {
            obj.isShowShanchu = YES;
        } else {
            obj.isShowShanchu = NO;
        }
        
        shareF.share = obj;
        [self.dataArray addObject:shareF];
    }];
    [self.tableView reloadData];

}
- (void)handleShare:(NSNotification *)notice
{
    NSArray *shareIdArray = notice.object;
    
    for (NSString *shareId in shareIdArray) {
        __block EFengShare *share = nil;
        [self.shareDataArray enumerateObjectsUsingBlock:^(EFengShare *obj, NSUInteger idx, BOOL *stop) {
            if ([obj.shareId isEqualToString:shareId]) {
                share = obj;
                *stop = YES;
            }
        }];
        if (share) {
        [self.shareDataArray removeObject:share];    
        }
        
    }

    
    [self.dataArray removeAllObjects];
    
    [self.shareDataArray enumerateObjectsUsingBlock:^(EFengShare *obj, NSUInteger idx, BOOL *stop) {
        EFengShareFrame *shareF = [[EFengShareFrame alloc] init];
        obj.contentReply = [self.commentMgr getShareCommentWithShareId:obj.shareId];
        obj.isZan = [self.commentMgr isHasZanIdWithShareId:obj.shareId userId:self.curContact.UserID];
        obj.praiseArray = [self.commentMgr getSharePraiseWithShareId:obj.shareId];
        if ([self.curContact.UserID isEqualToString:obj.releaseUserId]) {
            obj.isShowShanchu = YES;
        } else {
            obj.isShowShanchu = NO;
        }
        
        shareF.share = obj;
        [self.dataArray addObject:shareF];
    }];
    self.maxShare = self.dataArray.firstObject;
    self.minShare = self.dataArray.lastObject;
    [self.tableView reloadData];
}


- (void)WZAttributedActionDidClickForShowPersonalShare:(NSNotification *)notice
{
    [[EFengShareCommentToolBar defaultShareCommentToolBar] resetToolPositionToInitial];
    self.tabBarController.tabBar.hidden = NO;
    
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    EFengPersonalShareController *per = [[EFengPersonalShareController alloc] init];
    per.userId = notice.object;
    
    [nav pushViewController:per animated:YES];
    
}

- (void)releaseShare
{
    //bug fixed by lixu
   if ([self.curContact.UserID isEqualToString:self.Contact.UserID])
   {
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    nav.navigationBarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    EFengComposeController *compose = [[EFengComposeController alloc] init];
    [nav pushViewController:compose animated:YES];
   }
    else
    {
        CMineAttentionContact *attention = [self.attentionMgr loadAttentionContactWithUserId:self.Contact.UserID isAttentionMe:NO];
        NSString *str = attention ? @"取消关注":@"添加关注";
        self.moreView.chatListViewArray = @[str,@"发送消息",@"查看个人资料"];
        self.isShowMoreView = !self.isShowMoreView;
        if (self.isShowMoreView) {
            [self.moreView showChatListView];
        }
        else
        {
            [self.moreView hieChatListView];
        }
    }
}
#pragma Mark -- chatListViewDelegate
- (void)eFengChatListDidSelectWithIndexPath:(NSIndexPath *)indexPath
{
    //bug 9475 fixed by lixu
    [self.moreView hieChatListView];
    self.isShowMoreView = NO;
    switch (indexPath.row) {
        case 0:
        {
            
            __weak typeof(self) weakSelf = self;
            __block CMineAttentionContact *attention = [self.attentionMgr loadAttentionContactWithUserId:self.Contact.UserID isAttentionMe:NO];
            if (attention) {
                [[CXmppRequestMgr shareInstance] cancelMineAttentionWithUserId:attention.userId successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
                    
                    NSString *result = responseObject[@"result"];
                    if ([result isEqualToString:@"success"]) {
                        [MBProgressHUD showSuccess:@"已取消关注" toView:weakSelf.view];
                        [weakSelf.attentionMgr delAttentionContact:attention.userId];
                        [[NSNotificationCenter defaultCenter] postNotificationName:IMDIDUPDATEMINEATTENTION object:nil];
                    } else {
                        [MBProgressHUD showError:@"取消关注失败" toView:weakSelf.view];
                    }
                    
                    
                } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
                    [MBProgressHUD showError:@"取消关注失败" toView:weakSelf.view];
                }];
            } else {
                attention = [[CMineAttentionContact alloc] init];
                attention.userId = weakSelf.Contact.UserID;
                [[CXmppRequestMgr shareInstance] addMineAttentionWithUserId:weakSelf.Contact.UserID successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
                    NSString *result = responseObject[@"result"];
                    
                    if ([result isEqualToString:@"success"]) {
                        [MBProgressHUD showSuccess:@"已关注" toView:weakSelf.view];
                        [weakSelf.attentionMgr insertAttentionContact:attention];
                        [[NSNotificationCenter defaultCenter] postNotificationName:IMDIDUPDATEMINEATTENTION object:nil];
                    } else {
                        [MBProgressHUD showError:@"添加关注失败" toView:weakSelf.view];
                    }
                    
                } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
                    [MBProgressHUD showError:@"添加关注失败" toView:weakSelf.view];
                }];
            }
        }

            break;
        case 1:
        {
            CHChatRoomBase *chat = [[CHChatRoomBase alloc] init];
            chat.group = nil;
            chat.member = self.Contact;
            UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
            nav.navigationBarHidden = NO;
            self.navigationController.navigationBarHidden = YES;
            
            [nav pushViewController:chat animated:YES];
        }
            break;
        default:
        {
            GroupMemberDetailVC *vc = [[GroupMemberDetailVC alloc] init];
            vc.member = self.Contact;
            vc.isFromChat = NO;
            vc.isFromGroupChat = NO;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.moreView hieChatListView];
    self.isShowMoreView = NO;
    
}
- (void)loadData
{
    
    NSArray *array = [self.personShareMgr getShareListWithSenderUserId:self.Contact.UserID];
    if (array.count > 0) {
        
        [self.shareDataArray addObjectsFromArray:array];
        [self.dataArray removeAllObjects];
        [self.shareDataArray enumerateObjectsUsingBlock:^(EFengShare *obj, NSUInteger idx, BOOL *stop) {
            EFengShareFrame *shareF = [[EFengShareFrame alloc] init];
            obj.isFromMineShare = YES;
            //                obj.praiseCount = [NSString stringWithFormat:@"%zd",[self.commentMgr getSharePraiseAllReadCountWithShareId:obj.shareId]];
            obj.contentReply = [self.commentMgr getShareCommentWithShareId:obj.shareId];
            obj.isZan = [self.commentMgr isHasZanIdWithShareId:obj.shareId userId:self.curContact.UserID];
            obj.praiseArray = [self.commentMgr getSharePraiseWithShareId:obj.shareId];
            if ([self.curContact.UserID isEqualToString:obj.releaseUserId]) {
                obj.isShowShanchu = YES;
            } else {
                obj.isShowShanchu = NO;
            }
            
            shareF.share = obj;
            [self.dataArray addObject:shareF];
        }];

        self.maxShare = self.dataArray.firstObject;
        self.minShare = self.dataArray.lastObject;

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView.header endRefreshing];
            [self.tableView reloadData];
        });
            
    } else {
        NSString *date1 = [self.personShareMgr getMinDateShareWithUserId:self.Contact.UserID];
        NSString *curDate = [UIUtil getCurrentTimeStamp];
        if (!M_ISSTRNULL(date1)) {
            
        } else {
            date1 = curDate;
            //         date1 = @"1456816235900";
            EFengShare *share = [[EFengShare alloc] init];
            share.isLocal = YES;
            share.shareId = [NSString stringWithFormat:@"isLocal%@",date1];
            share.date = date1;
            [self.personShareMgr insertWithShare:share];
        }

        
      [[CXmppRequestMgr shareInstance] getShareListWithDate:date1 findType:@"down" userId:self.curContact.UserID releaseUserId:self.Contact.UserID successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
            if ([responseObject[@"result"] isEqualToString:@"success"]) {
                NSArray *array1 = responseObject[@"text"];
                NSArray *shareArray = [EFengShare objectArrayWithKeyValuesArray:array1];
                [self.personShareMgr batchInsertWithShareList:shareArray];
                [self.personShareMgr updateAllUnreadShareToReadShare];
                
                NSArray *array2 = [self.personShareMgr getShareListWithSenderUserId:self.Contact.UserID];
                if (array2.count > 0) {
                
                    [self.shareDataArray addObjectsFromArray:array2];
                    [self.dataArray removeAllObjects];
                    [self.shareDataArray enumerateObjectsUsingBlock:^(EFengShare *obj, NSUInteger idx, BOOL *stop) {
                        EFengShareFrame *shareF = [[EFengShareFrame alloc] init];
                        obj.isFromMineShare = YES;
//                        obj.praiseCount = [NSString stringWithFormat:@"%zd",[self.commentMgr getSharePraiseAllReadCountWithShareId:obj.shareId]];
                        obj.contentReply = [self.commentMgr getShareCommentWithShareId:obj.shareId];
                        obj.isZan = [self.commentMgr isHasZanIdWithShareId:obj.shareId userId:self.curContact.UserID];
                        obj.praiseArray = [self.commentMgr getSharePraiseWithShareId:obj.shareId];
                        if ([self.curContact.UserID isEqualToString:obj.releaseUserId]) {
                            obj.isShowShanchu = YES;
                        } else {
                            obj.isShowShanchu = NO;
                        }

                        shareF.share = obj;
                        [self.dataArray addObject:shareF];
                    }];
                    self.maxShare = self.dataArray.firstObject;
                    self.minShare = self.dataArray.lastObject;

                }
            }
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView.footer endRefreshing];
                [self.tableView reloadData];
            });
            
            
        } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView.footer endRefreshing];
                [self.tableView reloadData];
            });
            
        }];
        
    }
    
}


- (void)loadMoreData
{
    NSArray *array = nil;
    if (self.maxShare.share.date == nil || self.minShare.share.date == nil) {      
    } else {
        array = [self.personShareMgr getOlderShareWithMinShareTimeStamp:self.minShare.share.date releaseUserId:self.userId];
        if (array.count > 0) {
            NSMutableArray *arrayM = [NSMutableArray array];
            [arrayM addObjectsFromArray:self.shareDataArray];
            [arrayM addObjectsFromArray:array];
            self.shareDataArray = arrayM;
            [self.dataArray removeAllObjects];
            
            [self.shareDataArray enumerateObjectsUsingBlock:^(EFengShare *obj, NSUInteger idx, BOOL *stop) {
                EFengShareFrame *shareF = [[EFengShareFrame alloc] init];
//                obj.praiseCount = [NSString stringWithFormat:@"%zd",[self.commentMgr getSharePraiseAllReadCountWithShareId:obj.shareId]];
                obj.contentReply = [self.commentMgr getShareCommentWithShareId:obj.shareId];
                obj.isZan = [self.commentMgr isHasZanIdWithShareId:obj.shareId userId:self.curContact.UserID];
               obj.praiseArray = [self.commentMgr getSharePraiseWithShareId:obj.shareId];                obj.isFromMineShare = YES;
                if ([self.curContact.UserID isEqualToString:obj.releaseUserId]) {
                    obj.isShowShanchu = YES;
                } else {
                    obj.isShowShanchu = NO;
                }
                
                shareF.share = obj;
                [self.dataArray addObject:shareF];
            }];
            self.maxShare = self.dataArray.firstObject;
            self.minShare = self.dataArray.lastObject;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView.footer endRefreshing];
                [self.tableView reloadData];
            });
            
            
        } else {
            NSString *date1 = [self.personShareMgr getMinDateShareWithUserId:self.Contact.UserID];
            NSString *curDate = [UIUtil getCurrentTimeStamp];
            if (!M_ISSTRNULL(date1)) {
                
            } else {
                date1 = curDate;
                //         date1 = @"1456816235900";
                EFengShare *share = [[EFengShare alloc] init];
                share.isLocal = YES;
                share.shareId = [NSString stringWithFormat:@"isLocal%@",date1];
                share.date = date1;
                [self.personShareMgr insertWithShare:share];
            }
            
            
           [[CXmppRequestMgr shareInstance] getShareListWithDate:date1 findType:@"down" userId:self.curContact.UserID releaseUserId:self.Contact.UserID successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
                
                if ([responseObject[@"result"] isEqualToString:@"success"]) {
                    NSArray *array1 = responseObject[@"text"];
                    NSArray *shareArray = [EFengShare objectArrayWithKeyValuesArray:array1];
                    if (shareArray.count > 0) {
                        [self.personShareMgr batchInsertWithShareList:shareArray];
                        [self.personShareMgr updateAllUnreadShareToReadShare];
                        NSArray *array = [self.personShareMgr getOlderShareWithMinShareTimeStamp:self.minShare.share.date releaseUserId:self.userId];
                        if (array.count > 0) {
                            
                            NSMutableArray *arrayM = [NSMutableArray array];
                            [arrayM addObjectsFromArray:self.shareDataArray];
                            [arrayM addObjectsFromArray:array];
                            self.shareDataArray = arrayM;
                            [self.dataArray removeAllObjects];
                            [self.shareDataArray enumerateObjectsUsingBlock:^(EFengShare *obj, NSUInteger idx, BOOL *stop) {
                                EFengShareFrame *shareF = [[EFengShareFrame alloc] init];
                                obj.contentReply = [self.commentMgr getShareCommentWithShareId:obj.shareId];
                                obj.isZan = [self.commentMgr isHasZanIdWithShareId:obj.shareId userId:self.curContact.UserID];
                               obj.praiseArray = [self.commentMgr getSharePraiseWithShareId:obj.shareId];
                                if ([self.curContact.UserID isEqualToString:obj.releaseUserId]) {
                                    obj.isShowShanchu = YES;
                                } else {
                                    obj.isShowShanchu = NO;
                                }
                                obj.isFromMineShare = YES;
                                shareF.share = obj;
                                [self.dataArray addObject:shareF];
                            }];
                            self.maxShare = self.dataArray.firstObject;
                            self.minShare = self.dataArray.lastObject;

                        }
                    }
                    
                }
                
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView.footer endRefreshing];
                    [self.tableView reloadData];
                });
                
                
            } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView.footer endRefreshing];
                    [self.tableView reloadData];
                });
                
            }];
        }
    }
    
}

- (void)loadNewData
{
    NSArray *array = [self.personShareMgr getNewerShareWithMaxTimeStamp:self.maxShare.share.date releaseUserId:self.Contact.UserID];
    NSMutableArray *arrayM = [NSMutableArray array];
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
        [arrayM addObjectsFromArray:array];
        [arrayM addObjectsFromArray:self.shareDataArray];
        [self.dataArray removeAllObjects];
        self.shareDataArray = arrayM;
        
        [self.shareDataArray enumerateObjectsUsingBlock:^(EFengShare *obj, NSUInteger idx, BOOL *stop) {
            EFengShareFrame *shareF = [[EFengShareFrame alloc] init];
            obj.isFromMineShare = YES;
            obj.contentReply = [self.commentMgr getShareCommentWithShareId:obj.shareId];
            obj.isZan = [self.commentMgr isHasZanIdWithShareId:obj.shareId userId:self.curContact.UserID];
            obj.praiseArray = [self.commentMgr getSharePraiseWithShareId:obj.shareId];            if ([self.curContact.UserID isEqualToString:obj.releaseUserId]) {
                obj.isShowShanchu = YES;
            } else {
                obj.isShowShanchu = NO;
            }
            shareF.share = obj;
            [self.dataArray addObject:shareF];
        }];
        self.maxShare = self.dataArray.firstObject;
        self.minShare = self.dataArray.lastObject;

        [self.tableView.header endRefreshing];
        [self.tableView reloadData];
        
        
//    });
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    
    if (IsIOS7) {
        [self.navigationController.navigationBar setBarTintColor:RGBCOLOR(44, 170, 253)];
    } else {
        [self.navigationController.navigationBar setTintColor:RGBCOLOR(44, 170, 253)];
    }
    
    EFengShareCommentToolBar *tool = [EFengShareCommentToolBar defaultShareCommentToolBar];
    
    tool.eFengShareCommentToolBarDelegate = self;
    CGRect fr = tool.frame;
    fr.origin.y = HARDWARE_SCREEN_HEIGHT - 64.0f;
    tool.frame = fr;
    [self.view addSubview:tool];
    
    
    EFengShareEmotionView *emo = [EFengShareEmotionView defaultShareEmotionView];
    [self.view addSubview:emo];
    
 }

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    EFengShareCommentToolBar *tool = [EFengShareCommentToolBar defaultShareCommentToolBar];
    tool.eFengShareCommentToolBarDelegate = nil;
    [tool removeFromSuperview];
    
    [[EFengShareEmotionView defaultShareEmotionView] removeFromSuperview];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:WZAttributedActionDidClick object:nil];
}

#pragma mark tableView代理以及数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      //bug 8999 fixed by lixu
    if (self.dataArray.count>0) {
        self.noticeLable.hidden = YES;
    }
    else
    {
        self.noticeLable.hidden = NO;
    }
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.dataArray[indexPath.row] isKindOfClass:[NSString class]]) {
        return 50;
    }
  else
  {
    EFengShareFrame *share = self.dataArray[indexPath.row];
    return share.cellHeight;
  }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.dataArray[indexPath.row] isKindOfClass:[NSString class]]) {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.textLabel.text = self.dataArray[indexPath.row];
        return cell;
    }
    else
    {
    EFengShareFrame *share = self.dataArray[indexPath.row];
    EFengShareCell *cell = [EFengShareCell eFengShaeCellWithTableView:tableView];
    cell.cellIndexPath = indexPath;
    cell.eFengShareCellDelegate = self;
    cell.shareFrame = share;
    return cell;
    }
}
//bug 9130 fix by lixu
- (void)eFengShareCellDidClickHuiFuWithShare:(EFengShareFrame *)share indexPath:(NSIndexPath *)inedexPath
{
    self.selectedShare = share.share;
    self.selectedComment = nil;
    EFengShareCommentToolBar *toolBar = [EFengShareCommentToolBar defaultShareCommentToolBar];
    if ([toolBar.contentText isFirstResponder] || !toolBar.isEmotionShow) {
        [toolBar resetToolPositionToInitial];
        
    } else {
        [toolBar.contentText becomeFirstResponder];
        toolBar.contentText.placeholder = @"回复...";
        
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:inedexPath];
        CGPoint pt = self.tableView.contentOffset;
        CGFloat cellHeit = CGRectGetMaxY(cell.frame) + CGRectGetMaxY(self.topView.frame) - pt.y;
        if (cellHeit > toolBar.y) {
            
            
            pt.y = pt.y + cellHeit - toolBar.y;
            [self.tableView setContentOffset:pt animated:NO];
            
            
        } else {
            
            
        }
    }
    
}
//bug 9645 fixed by lixu
- (void)eFengShareCellDidClickOpenWithShare:(EFengShareFrame *)share indexPath:(NSIndexPath *)inedexPath
{
        [self.dataArray removeAllObjects];
        [self.shareDataArray enumerateObjectsUsingBlock:^(EFengShare *obj, NSUInteger idx, BOOL *stop) {
            
            if ([obj.shareId isEqualToString:share.share.shareId]) {
                obj.isOpen = !obj.isOpen;
            }
            
            EFengShareFrame *shareF = [[EFengShareFrame alloc] init];
            
            obj.contentReply = [self.commentMgr getShareCommentWithShareId:obj.shareId];
            obj.isZan = [self.commentMgr isHasZanIdWithShareId:obj.shareId userId:self.curContact.UserID];
            obj.praiseArray = [self.commentMgr getSharePraiseWithShareId:obj.shareId];
            if ([self.curContact.UserID isEqualToString:obj.releaseUserId]) {
                obj.isShowShanchu = YES;
            } else {
                obj.isShowShanchu = NO;
            }
            
            shareF.share = obj;
            [self.dataArray addObject:shareF];
        }];
    
    [self.tableView reloadData];
    
}

- (void)eFengshareCommentsViewDidHuifuWithComment:(EFengContent *)comment share:(EFengShareFrame *)share indexPath:(NSIndexPath *)inedexPath
{
    
    self.selectedShare = share.share;
    self.selectedComment = comment;
    if ([comment.sendUserId isEqualToString:self.curContact.UserID]) {
        
        if ([[EFengShareCommentToolBar defaultShareCommentToolBar].contentText isFirstResponder] || ![EFengShareCommentToolBar defaultShareCommentToolBar].isEmotionShow) {
            [[EFengShareCommentToolBar defaultShareCommentToolBar] resetToolPositionToInitial];
            self.tabBarController.tabBar.hidden = NO;
        }
        
        
        RIButtonItem *cancelItem = [RIButtonItem itemWithLabel:@"取消" action:^{
            
        }];
        
        __block EFengPersonalShareController *block = self;
        
        RIButtonItem *selectItem = [RIButtonItem itemWithLabel:@"删除" action:^{
          
            [block.commentMgr updateShareCommentIsUnNetCompletedWithComment:comment isDel:YES];
            [[CXmppRequestMgr shareInstance] cancelPraiseShareWithCommentId:comment.commentId date:comment.date successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
                
                if ([responseObject[@"result"] isEqualToString:@"success"]) {
                    
                    [block.commentMgr delShareCommentWithCommentId:comment.commentId];
                }
                
            } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
                
            }];
            [self loadNewData];
        }];
        
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil cancelButtonItem:cancelItem destructiveButtonItem:nil otherButtonItems:selectItem, nil];
        [actionSheet showInView:self.view];
        
        
    } else {
        if ([[EFengShareCommentToolBar defaultShareCommentToolBar].contentText isFirstResponder] || ![EFengShareCommentToolBar defaultShareCommentToolBar].isEmotionShow) {
            [[EFengShareCommentToolBar defaultShareCommentToolBar] resetToolPositionToInitial];
            
        } else {
            [[EFengShareCommentToolBar defaultShareCommentToolBar].contentText becomeFirstResponder];
            [[EFengShareCommentToolBar defaultShareCommentToolBar].contentText setText:@""];
            [[EFengShareCommentToolBar defaultShareCommentToolBar].contentText setPlaceholder:[NSString stringWithFormat:@"回复%@...",comment.sendUserName]];
            
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:inedexPath];
            CGPoint pt = self.tableView.contentOffset;
            CGFloat cellHeit = CGRectGetMaxY(cell.frame) + CGRectGetMaxY(self.topView.frame) - pt.y;
            if (cellHeit > [EFengShareCommentToolBar defaultShareCommentToolBar].y) {
                pt.y = pt.y + cellHeit - [EFengShareCommentToolBar defaultShareCommentToolBar].y;
                [self.tableView setContentOffset:pt animated:NO];
            }
        }
    }
    
}
- (void)eFengShareCellDidClickShanChuWithShare:(EFengShareFrame *)share indexPath:(NSIndexPath *)inedexPath
{
    if (![UIUtil isNetworkReachable]) {
        [MBProgressHUD showError:@"请检查网络设置"];
        return;
    }
    __block EFengPersonalShareController *block = self;
    JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:nil message:@"确定要删除该分享内容?"];
    [alert addButton:Button_OK withTitle:@"是" handler:^(JKAlertDialogItem *item) {
        
        [block.shareMgr updateShareIsUnNetCompletedWithShare:share.share isDel:YES];
        [block.personShareMgr delShareWithShareId:share.share.shareId];
        [[CXmppRequestMgr shareInstance] delShareWithDate:share.share.date shareId:share.share.shareId successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            if ([responseObject[@"result"] isEqualToString:@"success"]) {
                [block.shareMgr delShareWithShareId:share.share.shareId];
            }
            
        } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
        [block.dataArray removeObject:share];
        [block.shareDataArray removeObject:share.share];
        [block.tableView reloadData];
        
        
    }];
    
    [alert addButton:Button_CANCEL withTitle:@"否" handler:^(JKAlertDialogItem *item) {
        
        
    }];
    [alert show];
    
//    [self loadNewData];
}
- (void)eFengShareCellDidClickZanWithShare:(EFengShareFrame *)share indexPath:(NSIndexPath *)inedexPath
{
    if (share.share.isZan) {
        EFengContent *comment = [self.commentMgr getZanIdWithShareId:share.share.shareId userId:self.curContact.UserID];
            [self.commentMgr updateShareCommentIsUnNetCompletedWithComment:comment isDel:YES];
            [[CXmppRequestMgr shareInstance] cancelPraiseShareWithCommentId:comment.commentId date:comment.date successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
                
                if ([responseObject[@"result"] isEqualToString:@"success"]) {
                    
                    [self.commentMgr delShareCommentWithCommentId:comment.commentId];
                }
                
            } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
                
            }];
    } else {
        EFengContent *comment = [[EFengContent alloc] init];
        NSString *timeStamp = [UIUtil getCurrentTimeStamp];
        CServerSettingModel *serverSetting = [[CFolderMgr shareInstance].serSettingMgr getSetting];
        NSString *idkey = serverSetting.yuLiuZiduan1;
        if (M_ISSTRNULL(idkey)) {
            idkey = @"111111";
        }
        NSString *idStr = [NSString stringWithFormat:@"%@%@",timeStamp,idkey];

        comment.commentId = idStr;
        comment.shareId = share.share.shareId;
        comment.sendUserId = self.curContact.UserID;
        comment.yuLiuZiduan2 = 1;
        comment.sendUserName = self.curContact.UserName;
        comment.commentType = @"praise";
        comment.isClick = YES;
        comment.isRead = YES;
        comment.date = timeStamp;
        [self.commentMgr insertWithShareComment:comment];
        [self loadNewData];
        
        [[CXmppRequestMgr shareInstance] praiseShareWithSendUserId:self.curContact.UserID commentId:idStr releaseUserId:share.share.releaseUserId contentId:share.share.shareId sendUserName:self.curContact.UserName successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
            if ([responseObject[@"result"] isEqualToString:@"success"]) {
                [self.commentMgr updateCommentWithCommentId:[NSString stringWithFormat:@"%@",responseObject[@"id"]]
                                                            date:[NSString stringWithFormat:@"%@",responseObject[@"date"]]];
            }
        } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
        
    }
    [self loadNewData];
}

- (void)sendMsg:(NSString *)txtMsg
{
    [[EFengShareCommentToolBar defaultShareCommentToolBar] resetToolPositionToInitial];
    self.tabBarController.tabBar.hidden = NO;
    if (M_ISSTRNULL(txtMsg)) {
        return;
    }
    NSString *type = nil;
    NSString *receiveUserId = nil;
    NSString *receiveUserName = nil;
    if (self.selectedComment) {
        type = @"reply";
        receiveUserId = self.selectedComment.sendUserId;
        receiveUserName = self.selectedComment.sendUserName;
    } else {
        type = @"comment";
    }
    
    EFengContent *content = [[EFengContent alloc] init];
    NSString *timeStamp = [UIUtil getCurrentTimeStamp];
    CServerSettingModel *serverSetting = [[CFolderMgr shareInstance].serSettingMgr getSetting];
    NSString *idkey = serverSetting.yuLiuZiduan1;
    if (M_ISSTRNULL(idkey)) {
        idkey = @"111111";
    }
    NSString *idStr = [NSString stringWithFormat:@"%@%@",timeStamp,idkey];

    content.commentId = idStr;

    content.commentType = type;
    content.sendUserId = self.curContact.UserID;
    content.sendUserName = self.curContact.UserName;
    content.shareId = self.selectedShare.shareId;
    content.receiveUserId = receiveUserId;
    content.receiveUserName = receiveUserName;
    content.content = txtMsg;
    content.isClick = YES;
    content.isRead = YES;
    content.yuLiuZiduan2 = 1;
    content.date = [UIUtil getCurrentTimeStamp];
    [self.commentMgr insertWithShareComment:content];
    
    content = [self.commentMgr getMaxShareComment];
    
    
    [[CXmppRequestMgr shareInstance] composeShareCommentWithShareId:self.selectedShare.shareId commentId:idStr releaseUserId:self.selectedShare.releaseUserId sendUserId:self.curContact.UserID sendUserName:self.curContact.UserName receiveUserId:M_STOREVALUE(receiveUserId) receiveUserName:M_STOREVALUE(receiveUserName) content:txtMsg type:type successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if ([responseObject[@"result"] isEqualToString:@"success"]) {
            [self.commentMgr updateCommentWithCommentId:[NSString stringWithFormat:@"%@",responseObject[@"id"]]
                                                        date:[NSString stringWithFormat:@"%@",responseObject[@"date"]]];
            [USER_DEFAULT setBool:YES forKey:@"sendOK"];
            [USER_DEFAULT synchronize];
        }
        
        
    } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    [self loadNewData];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [[EFengShareCommentToolBar defaultShareCommentToolBar] resetToolPositionToInitial];
    self.tabBarController.tabBar.hidden = NO;
}
- (void)didTap:(UITapGestureRecognizer *)tap
{
    [[EFengShareCommentToolBar defaultShareCommentToolBar] resetToolPositionToInitial];
    self.tabBarController.tabBar.hidden = NO;
    [self.moreView hieChatListView];
    self.isShowMoreView = NO;
}

- (void)emotionViewDidHidden
{
    [self.tabBarController.tabBar setHidden:NO];
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (self.isShowMoreView) {
        return NO;
    }
    return YES;
}

@end
